package com.jams.goodox.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.jams.goodox.R;

/**
 * Created by liwei on 2016/7/26 15:39
 * Email: liwei@uama.com.cn
 * Description:圆形的显示评分比例的控件
 */
public class RatingCircleView extends View {

    private Paint paint;
    private RectF percentRect;

    private float strokeWidth;
    private int primaryColor;
    private int ratingColor;
    private float percent;

    public RatingCircleView(Context context) {
        super(context);
        init(context, null);
    }

    public RatingCircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);


        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.RatingCircleView, 0, 0);

        float defaultStrokeWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 3, context.getResources().getDisplayMetrics());
        strokeWidth = a.getDimension(R.styleable.RatingCircleView_strokeWidth, defaultStrokeWidth);
        primaryColor = a.getColor(R.styleable.RatingCircleView_primaryColor, Color.parseColor("#dcdcdc"));
        ratingColor = a.getColor(R.styleable.RatingCircleView_ratingColor, ContextCompat.getColor(context, R.color.font_color_red));
        percent = a.getFloat(R.styleable.RatingCircleView_percent, 0);

        a.recycle();

        paint.setStrokeWidth(strokeWidth);

        percentRect = new RectF();
        percentRect.left = strokeWidth / 2;
        percentRect.top = strokeWidth / 2;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int paddingLeft = getPaddingLeft();
        int paddingTop = getPaddingTop();
        int paddingRight = getPaddingRight();
        int paddingBottom = getPaddingBottom();

        int contentWidth = getWidth() - paddingLeft - paddingRight;
        int contentHeight = getHeight() - paddingTop - paddingBottom;

        int width = contentWidth < contentHeight ? contentWidth : contentHeight;
        float radius = width / 2f;

        // 画基础颜色的圆
        paint.setColor(primaryColor);
        canvas.drawCircle(radius, radius, radius - strokeWidth / 2, paint);

        // 画评分比例的圆弧
        paint.setColor(ratingColor);
        percentRect.right = width - strokeWidth / 2;
        percentRect.bottom = width - strokeWidth / 2;
        // 计算评分角度
        float sweepAngle = -360 * percent;
        canvas.drawArc(percentRect, -90, sweepAngle, false, paint);
    }

    /**
     * 设置比例
     * @param percent 比例值
     */
    public void setPercent(float percent) {
        if (percent < 0) {
            this.percent = 0;
        } else if (percent > 1) {
            this.percent = 1;
        } else {
            this.percent = percent;
        }
        invalidate();
    }
}
